package com.demo.autotest.controller;

import com.demo.autotest.common.utils.PageUtils;
import com.demo.autotest.database.domain.Project;
import com.demo.autotest.database.domain.UserProject;
import com.demo.autotest.dto.PageDTO;
import com.demo.autotest.dto.UserDTO;
import com.demo.autotest.request.QueryRequest;
import com.demo.autotest.service.ProjectService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController // 定义一个controller类
@RequestMapping("/autotest/project")
public class ProjectController {

    @Resource
    private ProjectService projectService;

    @PostMapping("/save")
    public void saveUser(@RequestBody Project project){
        projectService.saveProject(project);
    }

    @PostMapping("/switch")
    public UserDTO switchProject(@RequestBody UserProject userProject){
        return projectService.switchProject(userProject);
    }

    @PostMapping("/delete")
    public void deleteProject(@RequestBody Project project){
        projectService.deleteProject(project.getId());
    }

    @PostMapping("/recover")
    public void recoverProject(@RequestBody Project project){
        projectService.recoverProject(project.getId());
    }

    @PostMapping("/list/{pageNum}/{pageSize}")
    public PageDTO<List<Project>> getProjectList(@PathVariable Integer pageNum,
                                                 @PathVariable Integer pageSize,
                                                 @RequestBody QueryRequest queryRequest,
                                                 HttpServletRequest request){
        String user = request.getSession().getAttribute("userId").toString();
        queryRequest.setRequestUser(user);
        Page<Object> page = PageHelper.startPage(pageNum, pageSize, true);
        return PageUtils.setPageInfo(page, projectService.getProjectList(queryRequest));
    }
}
